package com.runvp.handler;

import com.alibaba.fastjson.JSONObject;
import com.runvp.common.config.RunvpConfig;
import com.runvp.common.core.redis.RedisCache;
import com.runvp.common.core.redis.RedisLockClient;
import com.runvp.common.enums.LineStatus;
import com.runvp.common.enums.NettyCmd;
import com.runvp.common.enums.NettyType;
import com.runvp.common.model.tcp.MessageReceiveBody;
import com.runvp.common.model.tcp.MessageResBody;
import com.runvp.common.util.enc.aes128gcm.Aes128GcmUtils;
import com.runvp.common.utils.DateUtils;
import com.runvp.service.biz.domain.BizDevice;
import com.runvp.service.build.BuildQRCode;
import com.runvp.service.tcp.TcpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class CmdXLHandler {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private RedisLockClient lockClient;
    @Autowired
    private TcpService tcpService;
    /**
     * 设备心跳逻辑处理
     * @param parseBody
     * @return MessageResBody
     */
    public MessageResBody handle(MessageReceiveBody parseBody){
        log.info("设备心跳逻辑处理:::receiveBody:{}",JSONObject.toJSONString(parseBody));
        String batchNo = System.nanoTime()+"";
        long id = parseBody.getBodyJson().getLong("id");
        tcpService.addBizTcpRecord(parseBody.setBatchNo(batchNo));
        BizDevice bizDevice = tcpService.instanceIBizDeviceService().selectBizDeviceById(id);
        MessageResBody resBody = buildRes(id);
        if(null == bizDevice){
            log.error("设备未安装id:{}",id);
        }else {
            handleBizDevice(bizDevice);
            String sn = parseBody.getTerminalId();
            resBody.setTerminalId(sn);
            resBody.setDeviceId(bizDevice.getId());
        }
        tcpService.addBizTcpRecord(resBody.setBatchNo(batchNo));
        log.info("设备心跳逻辑处理:::resBody:{}",JSONObject.toJSONString(resBody));
        return resBody;
    }
    /**
     * 处理设备
     *
     * @return str
     */
    private void handleBizDevice(BizDevice device) {
        device.setLastUpdateTime(DateUtils.getNowDate());
        device.setLineStatus(LineStatus.ON_LINE.getCode());
        device.setQrCode(BuildQRCode.createCode(device.getDeviceCode(), RunvpConfig.getPreview() + "/wechat.html?baseSn=" + device.getDeviceCode()));
        tcpService.instanceIBizDeviceService().updateBizDevice(device);
        log.info("设备【{}】执行修改,执行时间:【{}】", device.getDeviceCode(), DateUtils.now());
    }
    /**
     * 默认报文
     *
     * @return MessageResBody
     */
    public MessageResBody buildRes(long id) {
        MessageResBody resBody = new MessageResBody(NettyType.SERVER_RECEIVE_DEVICE.getCode(), NettyCmd.XL.getCode());
        resBody.setDid("000000000000");
        resBody.setVersion("01");
        resBody.setBody(buildBodys(id)).buildRes();
        return resBody;
    }
    /**
     * 结构结果体
     * @return str
     */
    private String buildBodys(long id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        StringBuilder sb = new StringBuilder(Aes128GcmUtils.getInstance().encrypt(JSONObject.toJSONString(map)));
        return sb.toString().toUpperCase();
    }

}
